package com.wskeee.ec.components.skin
{
	import com.wskeee.ec.ens;
	import com.wskeee.ec.components.Alert;
	import com.wskeee.ec.components.Button;
	import com.wskeee.ec.containers.HBox;
	import com.wskeee.ec.containers.LayoutConst;
	import com.wskeee.ec.containers.VBox;
	import com.wskeee.ec.core.EComponent;
	import com.wskeee.ec.core.EGlobal;
	import com.wskeee.ec.styles.DefaultStyle;
	
	import flash.display.DisplayObject;
	import flash.display.LineScaleMode;
	import flash.display.Sprite;
	import flash.geom.Rectangle;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;

	public class AlertSkin extends BaseSkinDrawer
	{
		use namespace ens;
		//--------------------------------------------------------------------------
		//
		//  Variables
		//
		//--------------------------------------------------------------------------
		/**
		 * alert 最大宽度 
		 */		
		private const MAX_TEXT_WIDTH:Number = 300;
		
		private const MIN_TEXT_WIDTH:Number = 150;
		private const MIN_TEXT_HEIGHT:Number = 40;
		//-----------------------------------------------------------------
		//  private
		//-----------------------------------------------------------------
		private var mainBox:VBox;
		private var controlBox:HBox;
		private var bgPanel:Sprite;
		//--------------------------------------------------------------------------
		//
		//  Properties
		//
		//--------------------------------------------------------------------------
		//-----------------------------------------------------------------
		//  Alert public 
		//-----------------------------------------------------------------
		public var titleFiled:TextField;
		public var textFiled:TextField;
		public var enterButton:Button;
		public var cancelButton:Button;
		
		//-----------------------------------------------------------------
		//  private 
		//-----------------------------------------------------------------
		private function get clip():Sprite
		{
			return ui as Sprite;
		}
		private function get alert():Alert
		{
			return target as Alert;
		}
		//--------------------------------------------------------------------------
		//
		//  Method
		//
		//--------------------------------------------------------------------------
		//-----------------------------------------------------------------
		//  init
		//-----------------------------------------------------------------
		override public function init(target:EComponent, ui:*):void
		{
			super.init(target,ui);
			initBGPanel();
			initLayout();
			initTitleField();
			initTextField();
			initControlButton();
		}
		//-----------------------------------------------------------------
		//  bgpanel
		//-----------------------------------------------------------------
		private function initBGPanel():void
		{
			
		}
		
		//-----------------------------------------------------------------
		//  layout
		//-----------------------------------------------------------------
		private function initLayout():void
		{
			mainBox = new VBox();
			mainBox.paddingTop = 10;
			mainBox.paddingBottom = mainBox.paddingLeft = mainBox.paddingRight = 5;
			mainBox.verticalGap = 10;
			mainBox.y = 20;
			mainBox.horizontalAlign = LayoutConst.CENTER;
			clip.addChild(mainBox);
		}
		
		//-----------------------------------------------------------------
		//  titleField
		//-----------------------------------------------------------------
		private function initTitleField():void
		{
			titleFiled = new TextField();
			titleFiled.multiline = titleFiled.wordWrap = false;
			titleFiled.textColor = getStyle("titleColor") as uint;
			titleFiled.height = 20;
			titleFiled.x = titleFiled.y = 3;
			titleFiled.mouseEnabled = false;
			clip.addChild(titleFiled);
		}
		
		//-----------------------------------------------------------------
		//  textFiled
		//-----------------------------------------------------------------
		private function initTextField():void
		{
			textFiled = new TextField();
			textFiled.textColor = getStyle("textColor") as uint;
			mainBox.addChild(textFiled);
		}
		
		//-----------------------------------------------------------------
		//  controlButton
		//-----------------------------------------------------------------
		private function initControlButton():void
		{
			controlBox = new HBox();
			controlBox.horizontalGap = 10;
			mainBox.addChild(controlBox);
			
			enterButton = new Button();
			cancelButton = new Button();
			enterButton.label = Alert.enterLabel;
			cancelButton.label = Alert.cancelLabel;
		}
		
		//-----------------------------------------------------------------
		//  redraw
		//-----------------------------------------------------------------
		override public function redraw():void
		{
			var round:Number = 5;
			var skin:Object = getSkin("backgroundImage");
			if(bgPanel)
				clip.removeChild(bgPanel);
			if(!skin)
			{
				bgPanel = new Sprite();
				bgPanel.graphics.lineStyle(undefined);
				bgPanel.graphics.beginFill(getStyle("headBackgroundColor") as uint,getStyle("headBackgroundAlpha") as Number);
				bgPanel.graphics.drawRoundRectComplex(0,0,100,25,round,round,0,0);
				
				bgPanel.graphics.beginFill(getStyle("backgroundColor") as uint,getStyle("backgroundAlpha") as Number);
				bgPanel.graphics.drawRoundRectComplex(0,25,100,40,0,0,round,round);
				bgPanel.graphics.endFill();
				
				if(alert.border)
				{
					bgPanel.graphics.lineStyle(1,getStyle("borderColor") as uint,1,true,LineScaleMode.NONE);
					bgPanel.graphics.drawRoundRect(0,0,bgPanel.width,bgPanel.height,round,round);
				}
				bgPanel.scale9Grid = new Rectangle(round,25,bgPanel.width-round*2,bgPanel.height-(25+round));
			}else
			{
				bgPanel = skin as Sprite;
				
			}
			clip.addChildAt(bgPanel,0);
			alert.filters = alert.shadow ? [DefaultStyle.getShadow(2,false,45,1,10,10,0.5)] : [];
		}
		
		//-----------------------------------------------------------------
		//  commitProperties
		//-----------------------------------------------------------------
		override public function validateProperties():void
		{
			textFiled.autoSize = TextFieldAutoSize.LEFT;
			textFiled.wordWrap = false;
			textFiled.width = MIN_TEXT_WIDTH;
			textFiled.text = alert.text;
			if(textFiled.textWidth>MAX_TEXT_WIDTH)textFiled.width = MAX_TEXT_WIDTH;
			textFiled.wordWrap = true;
			if(textFiled.textHeight<MIN_TEXT_HEIGHT)textFiled.height = MIN_TEXT_HEIGHT;
			
			titleFiled.text = alert.title;
			titleFiled.width = textFiled.width;
			controlBox.removeAllChildren();
			
			/**
			 * 根据 alert 的 flag 值显示控制按钮
			 */
			if((alert.flag & Alert.ENTER) == Alert.ENTER)
				controlBox.addChild(enterButton);
			if((alert.flag & Alert.CANCEL) == Alert.CANCEL)
				controlBox.addChild(cancelButton);
		}
		
		//-----------------------------------------------------------------
		//  measure
		//-----------------------------------------------------------------
		override public function validateSize():void
		{
			mainBox.validateNow();
			alert.setActualSize(mainBox.width,mainBox.height+25);
		}
		
		//-----------------------------------------------------------------
		//  updateDisplayList
		//-----------------------------------------------------------------
		override public function validateDisplayList():void
		{
			bgPanel.width = alert.width;
			bgPanel.height = alert.height;
		}
	}
}